package com.example.howard.xiao7music.model;

import android.util.Log;

import com.example.howard.xiao7music.bean.FastJsonTools;
import com.example.howard.xiao7music.bean.PlayBean;
import com.example.howard.xiao7music.bean.SearchBean;
import com.example.howard.xiao7music.bean.SingerSongBean;
import com.example.howard.xiao7music.bean.SongBySearchBean;
import com.example.howard.xiao7music.bean.SongDownloadBean;
import com.example.howard.xiao7music.bean.SongRankingBean;
import com.example.howard.xiao7music.model.getbasedata.NetUtils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Howard on 2016/9/25.
 */
public class GetSearch extends GetRemoteSource {
    private GetRemoteSourceClass getRemoteSourceClass=new GetRemoteSourceClass();
    private FastJsonTools fastJsonTools=new FastJsonTools();
    private SearchBean searchBean;
    private SingerSongBean singerSongBean;
    private getSearchcallback searchcallback;
    private List<SongBySearchBean> songBySearchBeanList=new ArrayList<>();


    public GetSearch(GetRemoteSource.callback callback,getSearchcallback getSearchcallback) {
        super(callback);
        this.searchcallback=getSearchcallback;
    }

    @Override
    public void getSingerSong(String singerid) {
        super.getSingerSong(singerid);

    }

    @Override
    public void getSongRanking(int num) {
        super.getSongRanking(num);
    }

    @Override
    public void getSingerSongSearchbySinger(String query) {

        String path;
        path=getRemoteSourceClass.getSearch(query);
        try {
            NetUtils.get(path, new NetUtils.AbsCallback() {
                @Override
                public void response(String url, byte[] bytes) {
                    super.response(url, bytes);
                    String json = new String(bytes);
                    String Json = json.trim();
                    Log.e("getsearch",Json);
                    SearchBean bean = fastJsonTools.createJsonBean(Json, SearchBean.class);
                    getSingerid(bean);

                }
            });
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


    }

    private void getSingerid(SearchBean bean) {
        SingerSongBean singerBean=new SingerSongBean();
        SearchBean searchBean ;
        searchBean = bean;
        String singerid;
        if (searchBean.getArtist() != null) {
            for (int i=0;i<searchBean.getArtist().size();i++){
                singerid = searchBean.getArtist().get(i).getArtistid();
                getsongbysingerid(singerid);
            }

        }

    }
    //根据Retrofit方法获得JSON返回数据，封装的方法更方便
    private void getsongbysingerid(String singerid) {
//        SingerSongBean bean;
        getSingerSong(singerid);
//        return bean;
    }



    @Override
    public void getSongSearchbySong(String query) {

        String path;
        path=getRemoteSourceClass.getSearch(query);
        try {
            NetUtils.get(path , new NetUtils.AbsCallback() {
                @Override
                public void response(String url, byte[] bytes) {
                    super.response(url, bytes);
                    String json = new String(bytes);
    //                Log.e("json",json.toString());
                    String Json=json.trim();
                    SearchBean bean= fastJsonTools.createJsonBean(Json,SearchBean.class);
    //                Log.e("bean",bean.toString());
                    setSearchBean(bean);
                    searchcallback.setSongBySearchBeanListBySong(songBySearchBeanList);
                }
            });
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    private void setSearchBean (SearchBean bean){
//        List<SongBySearchBean> list;
       SearchBean  searchBean;
        searchBean= bean;
//        Log.e("search",searchBean.toString());
        if (searchBean.getSong()!=null) {
            getsong(searchBean);
        }
//        return list;
    }

    //获取到歌曲的列表
    public void getsong(SearchBean searchBean){
//        List<SongBySearchBean> list=new ArrayList<>();
        songBySearchBeanList.clear();
        int i;
        for (i=0;i<searchBean.getSong().size();i++){
            SongBySearchBean songBySearchBean=new SongBySearchBean();
            songBySearchBean.setSongname(searchBean.getSong().get(i).getSongname());
            songBySearchBean.setSingername(searchBean.getSong().get(i).getArtistname());
            songBySearchBean.setSongid(searchBean.getSong().get(i).getSongid());
            songBySearchBeanList.add(songBySearchBean);
        }
//        return list;
    }



    public interface getSearchcallback{
        //回调得到bean对象
        void setSingerSongBeanSearchBySinger();
        //回调得到list
        void setSongBySearchBeanListBySong(List<SongBySearchBean> songBySearchBeanList);
    }


}
